/*
Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Some examples:
  ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
*/

class Solution {
public:
    int evalRPN(vector<string> &tokens) {
        stack<int> snumber;
        for (int i = 0; i < tokens.size(); i++) {
            char c = tokens[i][0];
            if (tokens[i].length() == 1 && // single char
                (c == '+' || c == '-' || c == '*' || c == '/')) 
            {
                int right = snumber.top(); snumber.pop();
                int left = snumber.top(); snumber.pop();
                snumber.push(Compute(left, right, c));
            } else {
                snumber.push(stoi(tokens[i]));
            }
        }
        return snumber.top();
    }
private:
    int Compute(int left, int right, char op) {
        switch (op) {
            case '+': return (left + right);
            case '-': return (left - right);
            case '*': return (left * right);
            case '/': return (left / right);
        }
        return 0;
    }
};
